package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CategotyMenuMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.CategoryMenuVO;
import com.itheima.service.CategoryMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class CategoryMenuServiceImpl implements CategoryMenuService {

    @Autowired
    private CategotyMenuMapper categotyMenuMapper;

    /**
     * 增加菜单
     * @param categoryMenuVO
     */
    @Override
    public void add(CategoryMenuVO categoryMenuVO) {
        CategoryMenu categoryMenu = JSON.parseObject(JSON.toJSONString(categoryMenuVO), CategoryMenu.class);
        //生成uid
        String uid = UUID.randomUUID().toString().replace("-","");
        categoryMenu.setUid(uid);
        categotyMenuMapper.insertSelective(categoryMenu);
    }

    /**
     * 删除菜单
     * @param categoryMenuVO
     */
    @Override
    public void delete(CategoryMenuVO categoryMenuVO) {
        Example example = createExample(categoryMenuVO);
        categotyMenuMapper.deleteByExample(example);
    }

    /**
     * 编辑菜单
     * @param categoryMenuVO
     */
    @Override
    public void edit(CategoryMenuVO categoryMenuVO) {
        CategoryMenu categoryMenu = JSON.parseObject(JSON.toJSONString(categoryMenuVO), CategoryMenu.class);
        Example example = new Example(CategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        if (categoryMenu.getUid() != null){
            criteria.andEqualTo("uid",categoryMenu.getUid());
        }
        categotyMenuMapper.updateByExample(categoryMenu,example);
    }

    /**
     * 获取所有列表(keyword不为空时)
     * @param keyword
     * @return
     */
    @Override
    public Map<String,Object> findAll(String keyword) {
        //根据参数获取等级为1的对象
        CategoryMenu levelOne = categotyMenuMapper.getLevelOne(keyword);
        //如果不存在返回空
        if (levelOne==null){
            return null;
        }
        //封装一级对象
        Map<String,Object> levelOneMap = JSON.parseObject(JSON.toJSONString(levelOne), Map.class);

        List<Map> levelTwoList=new ArrayList<>();

        //二级对象获取
        List<CategoryMenu> levelTwos = categotyMenuMapper.getByParentUidLevelTwo(keyword);

        //获取三级对象
        for (CategoryMenu levelTwo : levelTwos) {
            //封装二级对象
            Map<String,Object> levelTwoMap= JSON.parseObject(JSON.toJSONString(levelTwo), Map.class);
            List<CategoryMenu> levelThrees = categotyMenuMapper.getByParentUidLevelThree(levelTwo.getUid());
            levelTwoMap.put("childCategoryMenu",levelThrees);
            levelTwoList.add(levelTwoMap);
        }
        levelOneMap.put("childCategoryMenu",levelTwoList);
         //返回结果
        return levelOneMap;
    }

    /**
     * 获取所有列表(keyword为空时)
     *
     * @return
     */
    @Override
    public List<Map<String,Object>> getAll() {
        //获取所有一级对象
        List<CategoryMenu> levelOne = categotyMenuMapper.getAllLevelOne();
        if (levelOne==null){
            return null;
        }
        List<Map<String,Object>> list=new ArrayList<>();
        for (CategoryMenu categoryMenu : levelOne) {
            Map<String, Object> all = this.findAll(categoryMenu.getUid());
            list.add(all);
        }
        return list;
    }

    /**
     * 获取所有二级菜单-按钮列表
     * @param keyword
     * @return
     */
    @Override
    public List<CategoryMenu> getButtonAll(String keyword) {
        //判断二级菜单是否存在
        CategoryMenu levelTwo = categotyMenuMapper.getLevelTwo(keyword);
        if (levelTwo==null){
            //如果为空,则查询所有按钮菜单数据
            return categotyMenuMapper.findButtonList();
        }
        //存在则根据二级菜单uid进行查询
       return categotyMenuMapper.getButtonAll(keyword);
    }

    /**
     * 获取菜单列表
     * @param categoryMenuVO
     * @return
     */
    @Override
    public PageInfo<CategoryMenu> getList(CategoryMenuVO categoryMenuVO) {
        Integer currentPage = categoryMenuVO.getCurrentPage();
        Integer pageSize = categoryMenuVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(categoryMenuVO);

        List<CategoryMenu> categoryMenus = categotyMenuMapper.selectByExample(example);
        //返回结果
        return  new PageInfo<>(categoryMenus);
    }

    /**
     * 置顶菜单
     * @param categoryMenuVO
     */
    @Override
    public void stick(CategoryMenuVO categoryMenuVO) {
        String uid = categoryMenuVO.getUid();
        //获取数据库中排序字段最大值
        int max = categotyMenuMapper.getMax();
        //最大值+1然后存入数据库,达到指定操作
        categotyMenuMapper.stick(uid,max+1);
    }

    /*
    * 封装查询条件*/
    private Example createExample(CategoryMenuVO categoryMenuVO) {
        Example example=new Example(CategoryMenu.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("sort DESC");
        if(categoryMenuVO!=null) {

            if (categoryMenuVO.getIcon() != null && !"".equals(categoryMenuVO.getIcon())) {
                criteria.andEqualTo("icon", categoryMenuVO.getIcon());
            }
            if (categoryMenuVO.getIsShow() != null && !"".equals(categoryMenuVO.getIsShow())) {
                criteria.andEqualTo("isShow", categoryMenuVO.getIsShow());
            }

            if (categoryMenuVO.getMenuType() != null && !"".equals(categoryMenuVO.getMenuType())) {
                criteria.andEqualTo("menuType", categoryMenuVO.getMenuType());
            }

            if (categoryMenuVO.getName() != null && !"".equals(categoryMenuVO.getName())) {
                criteria.andEqualTo("name", categoryMenuVO.getName());
            }
            if (categoryMenuVO.getParentUid() != null && !"".equals(categoryMenuVO.getParentUid())) {
                criteria.andEqualTo("parentUid", categoryMenuVO.getParentUid());
            }

            if (categoryMenuVO.getUid() != null && !"".equals(categoryMenuVO.getUid())) {
                criteria.andEqualTo("uid", categoryMenuVO.getUid());
            }
            if (categoryMenuVO.getSummary() != null && !"".equals(categoryMenuVO.getSummary())) {
                criteria.andEqualTo("summary", categoryMenuVO.getSummary());
            }
            if (categoryMenuVO.getUrl() != null && !"".equals(categoryMenuVO.getUrl())) {
                criteria.andEqualTo("url", categoryMenuVO.getUrl());
            }
        }
        return example;
    }
}
